<?php 

// lop nay cua chu loc
class SanPhamDAO
{
	public static function PartseToSanPhamDTO($row)
	{
		$spDTO = new SanPhamDTO();
		$spDTO->sp_ma = $row["sp_ma"];
		$spDTO->sp_ten = $row["sp_ten"];
		$spDTO->sp_ngay_tao = $row["sp_ngay_tao"];
		$spDTO->sp_mo_ta = $row["sp_mo_ta"];
		$spDTO->sp_don_vi_tinh = $row["sp_don_vi_tinh"];
		$spDTO->sp_don_gia = $row["sp_don_gia"];
		$spDTO->sp_hinh_anh = $row["sp_hinh_anh"];
		$spDTO->sp_so_luong = $row["sp_so_luong"];
		$spDTO->sp_tinh_trang = $row["sp_tinh_trang"];
		$spDTO->sp_tong_luot_danh_gia = $row["sp_tong_luot_danh_gia"];
		$spDTO->sp_tong_so_sao = $row["sp_tong_so_sao"];
		$spDTO->sp_ma_gian_hang = $row["sp_ma_gian_hang"];
		return $spDTO;
	}
	
	public static function PartseToBinhLuanDTO($row)
	{
		$spDTO = new BinhLuanDTO();
		$spDTO->blsp_ma = $row["blsp_ma"];
		$spDTO->blsp_ma_san_pham = $row["blsp_ma_san_pham"];
		$spDTO->blsp_username = $row["blsp_username"];
		$spDTO->blsp_noi_dung = $row["blsp_noi_dung"];
		$spDTO->blsp_ngay_tao = $row["blsp_ngay_tao"];
		$spDTO->blsp_tong_so_sao = $row["blsp_tong_so_sao"];
		$spDTO->tk_avatar = $row["tk_avatar"];
		return $spDTO;
	}
	
	public static function PartseToSanPhamDTO1($row)
	{
		$spDTO = new SanPhamDTO();
		$spDTO->sp_ma = $row["sp_ma"];
		$spDTO->sp_ten = $row["sp_ten"];
		$spDTO->sp_ngay_tao = $row["sp_ngay_tao"];
		$spDTO->sp_mo_ta = $row["sp_mo_ta"];
		$spDTO->sp_don_vi_tinh = $row["sp_don_vi_tinh"];
		$spDTO->sp_don_gia = $row["sp_don_gia"];
		$spDTO->sp_hinh_anh = $row["sp_hinh_anh"];
		$spDTO->sp_so_luong = $row["sp_so_luong"];
		$spDTO->sp_tinh_trang = $row["sp_tinh_trang"];
		$spDTO->sp_tong_luot_danh_gia = $row["sp_tong_luot_danh_gia"];
		$spDTO->sp_tong_so_sao = $row["sp_tong_so_sao"];
		$spDTO->sp_ma_gian_hang = $row["sp_ma_gian_hang"];
		$spDTO->sp_ten_gian_hang =$row["gh_ten"];
		$spDTO->sp_chu_gian_hang=$row["gh_username"];
		return $spDTO;
	}
		
	public static function LaySamPhamTheoMaGianHang($maGianHang)
	{
		$sql = "SELECT * FROM san_pham WHERE sp_ma_gian_hang = '$maGianHang'";
		$result = QuerySQL($sql);
		
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$spDTO = SanPhamDAO::PartseToSanPhamDTO($row);
			$arraySpDTO[$i] = $spDTO;
			$i = $i + 1;
		}
		StopConnection();
		return $arraySpDTO;

	}
	public static function ChiTietSanPham($MaSanPham)
	{
		$sql = "SELECT * FROM san_pham sp,gian_hang gh WHERE sp_ma='$MaSanPham' and sp.sp_ma_gian_hang=gh.gh_ma";
		$result = QuerySQL($sql);
		while($row = mysql_fetch_array($result))
		{
			$spDTO = SanPhamDAO::PartseToSanPhamDTO1($row);
		}
		StopConnection();
		return $spDTO;
	}
	
	public static function CapNhatSanPhamTheoMa($sanphamDTO,$masp)
	{
		$sqlString = "UPDATE san_pham SET 
						
						sp_ten = '$spDTO->sp_ten',
						sp_ngay_tao = '$spDTO->sp_ngay_tao',
						sp_mo_ta = '$spDTO->sp_mo_ta',
						sp_don_vi_tinh = '$spDTO->sp_don_vi_tinh',
						sp_don_gia = '$spDTO->sp_don_gia',
						sp_hinh_anh = '$spDTO->sp_hinh_anh',
						sp_so_luong = '$spDTO->sp_so_luong',
						sp_tinh_trang = '$spDTO->sp_tinh_trang',
						sp_tong_luot_danh_gia = '$spDTO->sp_tong_luot_danh_gia',
						WHERE sp_ma = '$spDTO->sp_ma'";
		$result = QuerySQL($sqlString);
		return $result;
		
	}
	public static function ThemSanPhamMoi($sanphamDTO)
	{
		$sqlString = "insert into san_pham values( 						
						'$spDTO->sp_ten',
						'$spDTO->sp_ngay_tao',
						'$spDTO->sp_mo_ta',
						'$spDTO->sp_don_vi_tinh',
						'$spDTO->sp_don_gia',
						'$spDTO->sp_hinh_anh',
						'$spDTO->sp_so_luong',
						'$spDTO->sp_tinh_trang',
						'$spDTO->sp_tong_luot_danh_gia')";
		$result = QuerySQL($sqlString);
		return $result;
		
	}
	
	public static function HienThiBinhLuanSanPham($MaSanPham)
	{
		$sql = "SELECT * FROM binh_luan_san_pham,tai_khoan WHERE blsp_ma_san_pham='$MaSanPham' and blsp_username = tk_username";
		$result = QuerySQL($sql);
		$arraySpDTO = NULL;
		if($result !=NULL)
		{
			$i = 0;
			while($row = mysql_fetch_array($result))
			{
				$spDTO = SanPhamDAO::PartseToBinhLuanDTO($row);
				$arraySpDTO[$i] = $spDTO;
				$i = $i + 1;
			}
			StopConnection();
			return $arraySpDTO;
		}
		return NULL;
	}
	
	public static function BinhLuanSanPham($id,$user,$NoiDung,$NgayTao)
	{
		$sql = "INSERT INTO binh_luan_san_pham ( `blsp_ma_san_pham`, `blsp_username`, `blsp_noi_dung`,`blsp_ngay_tao`,`blsp_tong_so_sao`) VALUES ('$id','$user', '$NoiDung','$NgayTao',NULL)";
		$result = QuerySQL($sql);
		StopConnection();
		return $result;
	}
	////////////////////////////////////////////////////
	public static function DemSoLuongSanPhamTheoMaGianHang($maGianHang)
	{
		$sql = "SELECT count(*) as soluong FROM san_pham WHERE sp_ma_gian_hang = '$maGianHang' and sp_tinh_trang<>0";
		//echo($sql);
		$result = QuerySQL($sql);
		while($row = mysql_fetch_array($result))
		{
			$spDTO = $row["soluong"];
		}
		StopConnection();
		return $spDTO;

	}
	public static function LaySanPhamTheoMaGianHang($maGianHang,$start,$end)
	{
		$sql = "SELECT * FROM san_pham WHERE sp_ma_gian_hang = '$maGianHang' and sp_tinh_trang<>0 order by sp_ngay_tao DESC Limit $start,$end";
		$result = QuerySQL($sql);
		
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$spDTO = SanPhamDAO::PartseToSanPhamDTO($row);
			$arraySpDTO[$i] = $spDTO;
			$i = $i + 1;
		}
		StopConnection();
		return $arraySpDTO;

	}
	public static function DemSoLuongBinhLuan($masp)
	{
		$sql = "SELECT count(*) as soluong FROM binh_luan_san_pham Where blsp_ma_san_pham ='$masp'";
		$result = QuerySQL($sql);
		while($row = mysql_fetch_array($result))
		{
			$spDTO = $row["soluong"];
		}
		StopConnection();
		return $spDTO;
	}
	public static function ThemBinhLuanSanPham($masp,$username,$nd,$NgayTao,$tongsao)
	{
		$sql = "INSERT INTO binh_luan_san_pham ( `blsp_ma_san_pham`, `blsp_username`, `blsp_noi_dung`,`blsp_ngay_tao`,`blsp_tong_so_sao`) VALUES ('$masp','$username', '$nd','$NgayTao','$tongsao')";
		$result = QuerySQL($sql);
		StopConnection();
		return $result;
	}
}
?>
